#include <Windows.h>
#include <stdio.h>
#include <stdint.h>

#define IPC_BLOCK_NAME L"ACG_CIG_IPC_BLOCK"
#define IPC_EVENT_NAME L"ACG_CIG_IPC_EVENT"

typedef struct _IPC_BLOCK {
	void* Address;
	uint32_t PID;
	uint32_t RegionSize;
} IPC_BLOCK;

BOOL WINAPI DllMain(HINSTANCE hInstance, uint32_t dwReason, void* pReserved) {
	if (dwReason == DLL_PROCESS_ATTACH) {
		HANDLE hIPCMapping = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, IPC_BLOCK_NAME);

		printf("\r\n... DLL attached\r\n");

		if (hIPCMapping != NULL) {
			uint8_t* pIPCBuf = MapViewOfFile(hIPCMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(IPC_BLOCK));

			if (pIPCBuf != NULL) {
				IPC_BLOCK* pIPCBlock = pIPCBuf;
				HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION, FALSE, pIPCBlock->PID);

				printf("... chaning permissions of 0x%p in protected process %d to +RWX (%d bytes)\r\n", pIPCBlock->Address, pIPCBlock->PID, pIPCBlock->RegionSize);

				if (hProcess != NULL) {
					DWORD dwOldProtect = 0;

					printf("... successfully opened remote handle to PID %d\r\n", pIPCBlock->PID);

					if (VirtualProtectEx(hProcess, pIPCBlock->Address, pIPCBlock->RegionSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)) {
						HANDLE hEvent = OpenEventW(EVENT_MODIFY_STATE, FALSE, IPC_EVENT_NAME);

						if (SetEvent(hEvent)) {
							printf("... successfully sent signal event to protected process\r\n");
						}
						else {
							printf("... failed to send signal event to protected process (error 0x%08x)\r\n", GetLastError());
						}
					}
					else {
						printf("... failed to change permissions of remote memory at 0x%p (error 0x%08x)\r\n", pIPCBlock->Address, GetLastError());
					}
				}
			}
		}
	}

	return TRUE;
}